package com.fjrcloud.utils;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Author: Trun
 * Date: 2016-07-13.
 * Description: 上传工具类
 */
public class UploadUtils {

    // action配置文件路径
    public static final String ACTIONPATH = "config.properties";

    //文件上传存放的根目录
    /*public static String getFilePath() {

        // 属性文件
        Properties prop = new Properties();
        // 获取当前类加载的根目录，如：/C:/Program Files/Apache/Tomcat 6.0/webapps/fee/WEB-INF/classes/
        String path = null;
        try {
            path = UploadUtils.class.getClassLoader().getResource("").toURI().getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        // 把文件读入文件输入流，存入内存中
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(path + ACTIONPATH));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //加载文件流的属性
        try {
            prop.load(fis);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return prop.getProperty("uploadPath");

    }*/

    public static String getFilePath() {
        return ContextLoader.getCurrentWebApplicationContext().getServletContext()
                .getRealPath("/");
    }

    public static String upload(MultipartFile multipartFile, String fileUri, Object newName) {

        if (multipartFile == null || fileUri == null || "".equals(fileUri)) {
            return null;
        }

        fileUri = "/upload"+fileUri;

        String filePath = getFilePath() + fileUri;

        System.out.println("upload filePath = " + filePath);

        String oriFileName = multipartFile.getOriginalFilename();
        String newFileName;

        if (newName != null) {
            newFileName = newName + oriFileName.substring(oriFileName.lastIndexOf("."));
        } else {
            newFileName = oriFileName;
        }

        File fileDir = new File(filePath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }

        File file = new File(filePath + newFileName);
        try {
            if (file.exists()) {
                file.delete();
            }
            multipartFile.transferTo(file);
            return (fileUri + newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据文件路径，删除文件
     *
     * @param filePath 当前项目根目录下的路径
     * @return
     */
    public static boolean delFile(String filePath) {

        return new File(getFilePath() + filePath).delete();
    }

    /**
     * 根据文件路径，删除文件
     *
     * @param filePaths 当前项目根目录下的路径数组
     * @return
     */
    public static boolean delFile(String[] filePaths) {

        if (filePaths == null) {
            return false;
        }
        for (String s : filePaths) {
            delFile(s);
        }

        return true;
    }


    /**
     * 根据逗号分割字符串
     *
     * @param string 字符串
     * @return
     */
    public static String[] splitString(String string) {
        return string.split(",");
    }
}
